package com.anolesoft.fourteenWaiXie.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.fourteenWaiXie.dto.QuantityLogDTO;
import com.anolesoft.fourteenWaiXie.dto.QuantityManagementDTO;
import com.anolesoft.fourteenWaiXie.service.QuantityService;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value = "/waixieQuantity")
public class QuantityServiceController {

    @Autowired
    QuantityService quantityService;

    /**
     *
     * @param fabrecordid 制造记录条码
     * @return
     */
    @PostMapping(value = "/form")
    public AjaxResult queryQuantity(String fabrecordid){
        System.out.println("==============================="+fabrecordid);
        QuantityManagementDTO quantityManagementDTO = quantityService.queryQuantity(fabrecordid);
        if (StringUtils.isEmpty(quantityManagementDTO)){
            return AjaxResult.success("该制造记录条码不存在！");
        }
        if (quantityManagementDTO.getIsFromTemp() != 2){
            return AjaxResult.success("该条码不是外协制造记录条码！");
        }
        if(!quantityManagementDTO.getPartState().equals("2")){
            return AjaxResult.success("该条制造记录不是开工状态！");
        }

        //解决部分批架次字段为空值的问题
        String pjc = quantityManagementDTO.getPcjc();

        if (pjc == null || pjc.length() == 0){
            String pc = quantityManagementDTO.getPc();
            String jc = quantityManagementDTO.getJc();
            StringBuffer temp = new StringBuffer();
            if (pc == null || pc.length() == 0){
                quantityManagementDTO.setPcjc(pjc);
            } else if (jc == null || jc.length() == 0){
                quantityManagementDTO.setPcjc(pc);
            } else if (pc.length() == 5){
                String[] pcTemp = pc.split("\\-");
                String[] jcTemp = jc.split("\\-");
                for (int i = 0; i < pcTemp.length; i++){
                    temp.append(pcTemp[i]);
                    temp.append(jcTemp[i]);
                    if ((i+1) < pcTemp.length){
                        temp.append("-");
                    } else {
                        quantityManagementDTO.setPcjc(temp.toString());
                        temp.delete(0, temp.length());
                    }
                }
            } else if (pc.length() == 2 && jc.length() == 2){
                temp.append(pc);
                temp.append(jc);
                quantityManagementDTO.setPcjc(temp.toString());
                temp.delete(0, temp.length());
            } else {
                quantityManagementDTO.setPcjc(pc);
            }
        }
        return AjaxResult.success("查询成功",quantityManagementDTO);
    }

    @PostMapping(value = "/update")
    public AjaxResult updateQuantity(@RequestBody QuantityManagementDTO quantityManagementDTO){
        int num = Integer.parseInt(quantityManagementDTO.getNewTotal());
        int putNum = quantityManagementDTO.getPutNum();
        if (num < putNum) {
            modifyOtherNum(quantityManagementDTO,0);
            quantityManagementDTO.setPutNum(num);
            quantityManagementDTO.setNewAssignmentId(modifyPGH(quantityManagementDTO.getAssignmentId(), num));
            quantityManagementDTO.setPgh2(modifyPGH(quantityManagementDTO.getPgh2(), num));
        } else {
            num -= putNum;
            quantityManagementDTO.setNewAssignmentId(quantityManagementDTO.getAssignmentId());
            modifyOtherNum(quantityManagementDTO, num);
        }
        StringBuffer newTotal = new StringBuffer();
        newTotal.append("投产");
        newTotal.append(quantityManagementDTO.getPutNum());
        newTotal.append("；试棒");
        newTotal.append(quantityManagementDTO.getShibang());
        newTotal.append("；试料");
        newTotal.append(quantityManagementDTO.getShiliaoNum());
        newTotal.append("；试件");
        newTotal.append(quantityManagementDTO.getTrialNum());
        newTotal.append("；对刀");
        newTotal.append(quantityManagementDTO.getDdjNum());
        newTotal.append("；结余");
        newTotal.append(quantityManagementDTO.getJieyuNum());
        System.out.println(newTotal.toString());
        quantityManagementDTO.setNewTotal(newTotal.toString());
        int count = quantityService.updateQuantity(quantityManagementDTO);
        System.out.println("count:" + count);
        if (count == 1) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.success("修改失败，请重试");
        }
    }
    @PostMapping(value = "/log")
    public AjaxResult queryQuantityLog(@RequestBody QuantityLogDTO quantityLogDTO){
        PageData<List<QuantityLogDTO>> quantityLog = quantityService.queryQuantityLog(quantityLogDTO);
        System.out.println(">>>quantityLog:" + quantityLog.getTotal());
        PageResult pageResult = new PageResult(quantityLog.getItems(),quantityLogDTO.getPageSize(),quantityLogDTO.getPageNo(),quantityLog.getTotal());
        return AjaxResult.success("查询成功",pageResult);
    }

    public static QuantityManagementDTO modifyOtherNum(QuantityManagementDTO quantityManagementDTO, int num){
        int shibang = quantityManagementDTO.getShibang();
        int shiliao_num = quantityManagementDTO.getShiliaoNum();
        int trial_num = quantityManagementDTO.getTrialNum();
        int pohuai_num = quantityManagementDTO.getPohuaiNum();
        int ddj_num = quantityManagementDTO.getDdjNum();
        int jieyu_num = quantityManagementDTO.getJieyuNum();
        int temp = quantityManagementDTO.getTotalNum() - quantityManagementDTO.getPutNum();//试棒试料试件对刀结余破坏总数量
        int total = temp - num;//减少总数
        temp = jieyu_num + ddj_num;//对刀+结余
        if (total > temp) {
            quantityManagementDTO.setJieyuNum(0);
            quantityManagementDTO.setDdjNum(0);
            temp = total - temp;//试棒试料试件减少总数
            int[] maxNum = {0,0,0};//试棒试料试件最大减少数量数组
            if (shibang < pohuai_num) {
                maxNum[0] = 0;
                pohuai_num -= shibang;
            } else {
                maxNum[0] = shibang - pohuai_num;
                pohuai_num = 0;
            }
            if (shiliao_num < pohuai_num) {
                maxNum[1] = 0;
                pohuai_num -= shiliao_num;
                maxNum[2] = trial_num - pohuai_num;
            } else {
                maxNum[1] = shiliao_num - pohuai_num;
                maxNum[2] = trial_num;
            }
            System.out.println("====maxNum:" + maxNum[0] + "," + maxNum[1] + "," + maxNum[2]);
            //试棒试料试件数量修改
            if (maxNum[2] < temp) {
                temp -= maxNum[2];
                quantityManagementDTO.setTrialNum(trial_num - maxNum[2]);
            } else {
                quantityManagementDTO.setTrialNum(trial_num - temp);
                temp = 0;
            }
            if (maxNum[1] < temp) {
                temp -= maxNum[1];
                quantityManagementDTO.setShiliaoNum(shiliao_num - maxNum[1]);
                quantityManagementDTO.setShibang(shibang - temp);
            } else {
                quantityManagementDTO.setShiliaoNum(shiliao_num - temp);
            }
        } else if (total < jieyu_num) {
            quantityManagementDTO.setJieyuNum(jieyu_num - total);
        } else {
            quantityManagementDTO.setJieyuNum(0);
            quantityManagementDTO.setDdjNum(temp - total);
        }

        return quantityManagementDTO;
    }

    public static String modifyPGH(String s, int num){
        StringBuffer newPGH = new StringBuffer();
        String[] pgh_array = s.split(",");
        for (int i = 0; i < pgh_array.length; i++) {
            String[] temp = pgh_array[i].split("=");
            int j = 0;
            while (j < temp.length - 1){
                newPGH.append(temp[j]);
                newPGH.append("=");
                j++;
            }
            int assignmentId_num = Integer.parseInt(temp[j]);
            if (assignmentId_num <= num){
                newPGH.append(assignmentId_num);
                num -= assignmentId_num;
            } else {
                newPGH.append(num);
                num = 0;
            }
            if (i+1 < pgh_array.length){
                newPGH.append(",");
            }
        }
        System.out.println(">>>>>>>>>>>>修改后派工号：" + newPGH.toString());
        return newPGH.toString();
    }
}
